package pong.game.artificial_intelligence;

import pong.game.Data;
import pong.game.objects.GL_Washer;
import pong.mathematic.Vector3D;

public class BotEasy extends Bot {

	private static final String TAG = "BotEasy";

	private static final float MOVE_TO_DANGER_WASHER_SENSETIVITY = 30f;

	private static final float IMPULSE_RADIUS_PROCENT_DANGER_WASHER_DETECTED = 0.75f;
	private static final float IMPULSE_MAX_SPEED_DANGER_WASHER_FOR_IMPULSE_ACTIVATE = 1f;

	public BotEasy(String userName, Vector3D center, float width, float height) {
		super(userName, center, width, height);
	}

	@Override
	protected GL_Washer getDangerWasher() {
		Data data = Data.getInstance();
		GL_Washer dangerWasher = data.getWasherItem(0);
		if (dangerWasher == null)
			return null;

		float distanceToDangerWasher = Math.abs(getCenter().getY() - dangerWasher.getCenter().getY()) - getRadius()
				- dangerWasher.getRadius();

		int count = data.countWasherItems();
		for (int q = 1; q < count; q++) {
			GL_Washer washer = data.getWasherItem(q);

			float bufferDistance = Math.abs(getCenter().getY() - washer.getCenter().getY()) - getRadius()
					- washer.getRadius();
			if (bufferDistance < distanceToDangerWasher) {
				distanceToDangerWasher = bufferDistance;
				dangerWasher = washer;
			}
		}
		return dangerWasher;
	}

	@Override
	protected void moveToDangerWasher(GL_Washer dangerWasher, float timeFraction) {
		Vector3D power = new Vector3D(dangerWasher.getCenter());
		power.minus(getCenter());
		power.multy(MOVE_TO_DANGER_WASHER_SENSETIVITY * timeFraction);
		power.setY(0);
		getDirection().add(power);
	}

	@Override
	protected void impulseActivate(GL_Washer dangerWasher) {
		if (getImpulse().getRadius() * getImpulse().getScaleValue() * IMPULSE_RADIUS_PROCENT_DANGER_WASHER_DETECTED > getImpulse()
				.getCenter().distance(dangerWasher.getCenter()) - dangerWasher.getRadius()) {
			if (dangerWasher.getDirection().distance() < IMPULSE_MAX_SPEED_DANGER_WASHER_FOR_IMPULSE_ACTIVATE)
				impulse();
		}
	}
}
